/**
 * learning-gulp - gulpfile.js
 * Created by mengdesen on 15/4/14.
 * Last modified by nieweidong on 2015/04/15
 */

'use strict';

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var shrink = require('gulp-cssshrink');

var fconfig     = require('./config/config.json');
var sftp       = require('gulp-sftp');
var clean       = require('gulp-clean');
// 静态文件打包合并
var webpack = require('gulp-webpack');
// 上传七牛sdn
//var qn = require('gulp-qn');
// MD5戳
var rev = require('gulp-rev');
var revCollector = require('gulp-rev-collector');
var runSequence = require('run-sequence');

var config = require('./webpack.config');
// var qiniu = {
//   accessKey: '6sBCo463jJOCnBIYX__uy9avZ7C2hj_MHb-ffKAr',
//   secretKey: '3vPk7fB0HcwL5V9E2AErHuR19HM389eYqdvQcncL',
//   bucket: 'xdemo',
//   domain: 'http://7xik9a.com1.z0.glb.clouddn.com'
// };

gulp.task('js', function () {
  gulp.src('./js')
    .pipe(webpack(config))
    .pipe(gulp.dest('./build'));
});

gulp.task('css', function () {
  gulp.src(['./css/main.css', './css/view.css'])
    .pipe(concat('app.css'))
    .pipe(gulp.dest('./build'));
});

gulp.task('publish-js', function () {
  return gulp.src(['./js'])
    .pipe(webpack(config))
    .pipe(uglify())
    .pipe(rev())
    .pipe(gulp.dest('./build'))
    // .pipe(qn({
    //   qiniu: qiniu,
    //   prefix: 'gmap'
    // }))
    .pipe(rev.manifest())
    .pipe(gulp.dest('./build/rev/js'));
});

gulp.task('publish-css', function () {
  return gulp.src(['./css/main.css', './css/view.css'])
    .pipe(concat('app.css'))
    .pipe(shrink())
    .pipe(rev())
    .pipe(gulp.dest('./build'))
    // .pipe(qn({
    //   qiniu: qiniu,
    //   prefix: 'gmap'
    // }))
    .pipe(rev.manifest())
    .pipe(gulp.dest('./build/rev/css'));
});

// gulp.task('watch', function () {
//   gulp.watch('./css/*.css', ['css']);
//   gulp.watch('./js/*.js', ['js']);
// });


gulp.task('publish-html', function () {
  return gulp.src(['./build/rev/**/*.json', './index.html'])
    .pipe(revCollector({
      dirReplacements: {
        'build/': ''
      }
    }))
    .pipe(gulp.dest('./build/'));
});


gulp.task("clean", function(){
    return gulp.src('./build/**')
        .pipe(clean());
})

gulp.task('publish', function (callback) {
  runSequence('clean',
    ['publish-css', 'publish-js'],
    'publish-html',
    callback);
});


gulp.task('upload', function () {

  console.log(fconfig.sftp);

    return gulp.src('./build/**')
        .pipe(sftp({
            // host: fconfig.sftp.host,
            // user: fconfig.sftp.user,
            port: fconfig.sftp.port,
            // pass: fconfig.sftp.pass,
            // timeout: fconfig.sftp.timeout,
            // remotePath: fconfig.sftp.remotePath
            location:'/config',
            passphrase:'secretphrase',
            host: 'vhost201.cloudvhost.cn',
            auth: 'keyMain'
        }));
});


gulp.task('watch', function () { 
    gulp.watch(['index.html','./css/**'], ['publish']);

});

